package com.potato.base.observer;

import com.potato.base.mvp.IView;
import com.potato.network.bean.BaseEntity;
import com.potato.network.exception.CustomException;

import io.reactivex.observers.DisposableObserver;

public abstract class BaseObserver<T> extends DisposableObserver<BaseEntity<T>> {

    protected IView view;

    public BaseObserver(IView view) {
        this.view = view;
    }
    public BaseObserver() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        if (view!=null)
            view.showLoading();
    }

    @Override
    public void onNext(BaseEntity<T> tBaseEntity) {
        if (view!=null)
            view.hideLoading();
        if (!tBaseEntity.error){
            onSuccess(tBaseEntity);
        }else{
            if (tBaseEntity.results==null){
                onError("当前没有数据");
            }else{
                onError(tBaseEntity.message);
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        if (view!=null)
            view.hideLoading();
        onError(CustomException.handleException(e).displayMessage);
    }

    @Override
    public void onComplete() {

    }
    public abstract void onSuccess(BaseEntity<T> entity);
    public abstract void onError(String msg);

}
